package com.toobigtofail.images
{
	import fabulaexmachina.box2fp.Box2DWorld;
	
	import flash.geom.Rectangle;
	
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Spritemap;
	
	public class ShapeImage extends Spritemap
	{
		[Embed(source="/assets/gfx/box.png")] 	  private static const BOX_PNG:Class;
		[Embed(source="/assets/gfx/octagon.png")] private static const OCTAGON_PNG:Class;
		[Embed(source="/assets/gfx/plank.png")]   private static const PLANK_PNG:Class;
		[Embed(source="/assets/gfx/wedge.png")]   private static const WEDGE_PNG:Class;
		
		private var _isOffset:Boolean = false;
		private var _type:String;
		private var _shape:String;
		
		public function ShapeImage(shape:String, type:String="BuildingBlock") {
			var source:*, frameWidth:uint, frameHeight:uint;
			switch(shape) {
				case "box":     source = BOX_PNG;
						   		frameWidth = 30;
						   		frameHeight = 30;
						   		break;
				case "octagon": source = OCTAGON_PNG;
						   		frameWidth = 60;
						   		frameHeight = 60;
				 		   		break;
				case "plank":   source = PLANK_PNG;
						   		frameWidth = 30;
						   		frameHeight = 120;
						   		break;
				case "wedge":   source = WEDGE_PNG;
						   		frameWidth = 120;
						   		frameHeight = 60;
						   		break;
			}
			_shape = shape;
			
			super(source, frameWidth, frameHeight);
			
			this.type = type;
		}
		
		public function offsetForBox2D(width:Number, height:Number):void {
			if (_isOffset) return;
			
			var scale:Number = FP.world ? (FP.world as Box2DWorld).scale : 1;
			x -= (width*scale)/2;
			y -= (height*scale)/2;
			centerOrigin();
			
			_isOffset = true;
		}
		
		public function set type(type:String):void {
			_type = type;
			switch(type) {
				case "BuildingBlock": frame = 0; break;
				case "Target": 		  frame = 1; break;
				case "Enemy": 		  frame = 2; break;
				case "Obstacle": 	  frame = 3; break;
			}
		}
		
		public function get type():String { return _type; }
		public function get shape():String { return _shape; }
	}
}